package org.cuatrovientos.caesar;


/**
 * implements caeser cipher system
 * @author Caius Contomatus, legio XIII
 *
 */
public class caesar_cypher {
	private int shift;
	private String alphabet = "abcdefghijklmnopqrstuvwxyz";
	private String calf = "";
	
	public caesar_cypher (int sft) {
		this.shift = sft;
	}
	
	/**
	 * encrypts a String using caser cipher
	 * @param message
	 * @return
	 */
	public String encrypt (String message) {
		String encrpyptMessage = "";

		message = message.toLowerCase();
		int pos = 0;
		
		for (int i=0;i<message.length();i++) {
			pos = getAlf().indexOf(message.charAt(i));
			pos = (pos + shift) % 26; 
			encrpyptMessage += getAlf().charAt(pos);
		}
		
		return encrpyptMessage;
	}
	
	/**
	 * decrypts a String using caser cipher
	 * @param message
	 * @return
	 */
	public String decrpt (String message) {
		String decpryptedMessagem = "";
		setCalf("");
		message = message.toLowerCase();
		int position = 0;
		
		for (int i=0;i<message.length();i++) {
			position = getAlf().indexOf(message.charAt(i));
			position = (26 + (position - shift)) % 26; 
			decpryptedMessagem += getAlf().charAt(position);
		}
		
		return decpryptedMessagem;
	}

	/**
	 * @return the alf
	 */
	String getAlf() {
		return alphabet;
	}

	/**
	 * @param alf the alf to set
	 */
	void setAlf(String alf) {
		this.alphabet = alf;
	}

	/**
	 * @return the calf
	 */
	String getCalf() {
		return calf;
	}

	/**
	 * @param calf the calf to set
	 */
	void setCalf(String calf) {
		this.calf = calf;
	}

	/**
	 * @return the shift
	 */
	int getShift() {
		return shift;
	}

	/**
	 * @param shift the shift to set
	 */
	void setShift(int shift) {
		this.shift = shift;
	}

}
